﻿using ZyGame.Execute;
using ZyGame.Resource;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace ZyGame.UI
{
    public class UIListView : IDisposable
    {
        private GameObject listRoot;
        private GameObject template;
        private Dictionary<string, UIListViewItem> items;
        private Queue<UIListViewItem> cache;
        private List<string> sortList = new List<string>();

        public UIListView(GameObject gameObject, GameObject itemObject)
        {
            listRoot = gameObject;
            template = itemObject;
            items = new Dictionary<string, UIListViewItem>();
            cache = new Queue<UIListViewItem>();
            template.SetActive(false);
        }

        public UIListViewItem First()
        {
            if (sortList.Count is 0)
            {
                return default;
            }
            return items[sortList.First()];
        }

        public UIListViewItem Last()
        {
            if (sortList.Count is 0)
            {
                return default;
            }
            return items[sortList.Last()];
        }

        public int IndexOf(string key)
        {
            return sortList.IndexOf(key);
        }

        public void Dispose()
        {
            foreach (var item in items.Values)
            {
                item.Dispose();
            }
            items.Clear();
        }

        public UIListView Clear()
        {
            foreach (var item in items.Values)
            {
                item.Disable();
                cache.Enqueue(item);
            }
            sortList.Clear();
            items.Clear();
            return this;
        }

        public UIListViewItem AddItem(string key, object data)
        {
            UIListViewItem item = GetViewItem(key);
            if (item != null)
            {
                return item;
            }

            if (cache.TryDequeue(out item))
            {
                items.Add(key, item);
                sortList.Add(key);
                item.Init(data);
                item.Enable();
                return item;
            }
            GameObject temp = GameObject.Instantiate(template);
            temp.name = template.name;
            temp.AsChildOf(template.transform.parent.gameObject);
            RectTransform transform = temp.GetComponent<RectTransform>();
            transform.localPosition = Vector3.zero;
            transform.localRotation = Quaternion.identity;
            transform.localScale = Vector3.one;
            items.Add(key, item = new UIListViewItem(temp));
            sortList.Add(key);
            item.Init(data);
            item.Enable();
            return item;
        }

        public UIListView Sort(Comparison<UIListViewItem> comparison)
        {

            return this;
        }

        public UIListView Refresh()
        {
            return this;
        }

        public UIListView Remove(string key)
        {
            UIListViewItem item = GetViewItem(key);
            if (item != null)
            {
                item.Dispose();
                items.Remove(key);
                cache.Enqueue(item);
                sortList.Remove(key);
            }
            return this;
        }

        public UIListViewItem GetViewItem(string key)
        {
            if (items.TryGetValue(key, out UIListViewItem item))
            {
                return item;
            }
            return default;
        }

        public List<UIListViewItem> GetListViews()
        {
            return items.Values.ToList();
        }
    }
}
